<?php
namespace app\manage\controller;
use think\Db;
use app\model\Channel;
use think\Controller;

class Base extends Controller
{
   /**
    * Undocumented 菜单组装
    *
    * @param [type] $cate
    * @param string $lefthtml
    * @param integer $pid
    * @param integer $lvl
    * @param integer $leftpin
    * @return void
    */
    public function getMenu($cate , $lefthtml = '└─' , $pid=0 , $lvl=0, $leftpin=0){
		$arr=array();
		foreach ($cate as $v){
			if($v['pid']==$pid){
				$v['leftpin']=$leftpin + 0;//左边距
				$v['lefthtml']=str_repeat($lefthtml,$lvl);
				$arr[]=$v;
				$arr= array_merge($arr,self::getMenu($cate,$lefthtml,$v['id'],$lvl+1 , $leftpin+20));
			}
		}
		return $arr;
    }
    
    /**
     * Undocumented 渠道统计导出
     *
     * @return void
     */
    public function chan_export($data){
        $list = Db::name("chan_statistical")->alias('a')->where(function($query) use($data){
            //渠道
            if(!empty($data['channel'])){
                $query->where('a.channel_id',$data['channel']);
            }
            //时间
            if(!empty($data['start'])){
                $query->where('a.create_time',$data['start']);
            }
        })
        ->join('channel b','a.channel_id = b.id')
        ->field("
            b.title,
            a.pay_code as payment,
            a.create_time,
            a.amount,
            a.max_count,
            a.actual_amount,
            a.succ_num,
            a.err_pay_amount,
            a.error_num,
            a.settle,
            a.agent_amount,
            a.upstream_settle,
            a.platform
        ")->select();
        $title = [
            "渠道名称",
            "支付类型",
            "日期",
            "总请求数",
            "总笔数",
            "支付成功",
            "成功笔数",
            "未支付",
            "未支付笔数",
            "商户结算金额",
            "代理结算金额",
            "上游结算金额",
            "平台盈利"
        ];
        $this->export($list,'渠道统计', $title);
    }
   
    /**
     * Undocumented 平台统计导出
     *
     * @param [type] $list 数组
     * @param [type] $name  表名
     * @param [type] $title 表头
     * @return void
     */
    private function export($list,$name, $title){
        ini_set('max_execution_time', 100);// 设置PHP超时时间
        ini_set('memory_limit', '500M');// 设置PHP临时允许内存大小
         //路径
         $fileName = $name. date('Ymd_His') . '.csv';
         $filePath = '../' . $fileName;
         $index = 0;
         $fp = fopen($filePath, 'w'); //生成临时文件
         chmod($filePath, 0777);//修改可执行权限
        fputcsv($fp, $title);
        foreach ($list as  &$item) {
           
            foreach ($item as $k => $v) {
                $item[$k] = $v . "\t";
                //每次写入1000条数据清除内存
                if ($index == 10000) { 
                    $index = 0;
                    ob_flush();//清除内存
                    flush();
                }
                $index++;
            }
            fputcsv($fp, $item);
        }
        ob_flush();
        fclose($fp);  //关闭句柄
        header("Cache-Control: max-age=0");
        header("Content-type:application/vnd.ms-excel;charset=UTF-8");
        header("Content-Description: File Transfer");
        header('Content-disposition: attachment; filename=' . basename($fileName));
        header("Content-Type: text/csv");
        header("Content-Transfer-Encoding: binary");
        header('Content-Length: ' . filesize($filePath));
        @readfile($filePath);//输出文件;
        unlink($filePath); //删除压缩包临时文件
    }

}